import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy, globalConfig:{COVIDUrl} } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
// const $time 	= $Lazy.file(()=>import('@com/utils/time'))

const Sex = [{ name:'未知', value:2 },{ name:'男', value:0 },{ name:'女', value:1 }]
const Status = [{ name:'启用', value:0 },{ name:'禁用', value:-1 }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '状态',			name: 'status',		type: 'select', data:Status },
			{ label: '姓名',			name: 'real_name' },
			{ label: '帐号',			name: 'empl_name' },
			{ label: '邮箱编码',		name: 'email' },
			{ label: '手机号码',		name: 'phone' },
		],
		// 提交表单列表
		formData : [
			{ label: '帐号名称',		name: 'emplName', 			full:true,	required:true  },
			{ label: '真实姓名',		name: 'realName', 			half:true,	required:true},
			{ label: '密码',			name: 'password', 			half:true,	required:true,	type: 'password' },
			{ label: '确认密码',		name: 'confirm_password', 	half:true,	required:true,	type: 'password' },
			{ label: '手机号码',		name: 'phone', 				half:true,	required:true },
			{ label: '性别',			name: 'sex', 				half:true,	 type:'select',	data:Sex },
			{ label: '年龄',			name: 'age', 				half:true, 	 type:'number'  },
			{ label: '电子邮件',		name: 'email', 				half:true,  },
			{ label: '状态',			name: 'status', 			half:true,	 type:'select',	data:Status },
			// { label: '头像',			name: 'head_portrait', 		full:true, type:'upload', code:121 },
			{ label: '电子签名',		name: 'signature', 			full:true, type:'upload', code:121, param: () => this.signatureParam },		 // 上传电子签名添加source_come,uuid参数 2021-9-7  --马婷 
		]
	}
	
	model = {}
	signatureParam = {source_come: 'signature'}
	componentDidMount(){
		this.fetch()
	}

	// paging
	fetch = () => $fn.fetch.call(this, COVIDUrl+'SpUser/query',{COVID:true})
	
	// 表单
	cols = [
		{ title: '帐号名称',		dataIndex: 'emplName', 		width:100 },
		{ title: '真实姓名', 	dataIndex: 'realName', 		...$fn.table.name },
		{ title: '手机号码', 	dataIndex: 'phone', 		...$fn.table.phone },
		{ title: '邮箱地址', 	dataIndex: 'email', 		width:200 },
		{ title: '性别', 		dataIndex: 'sex', 			...$fn.table.sex,	render: text => $fn.getState(Sex, text) },
		{ title: '状态', 		dataIndex: 'status', 		...$fn.table.status,	render: text => $fn.getState(Status, text)},
		TableControls.controls.call(this, { width:150, align:'left'})
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, status }) => {
		const { formData } = this.state
		return [
			// 编辑
			TableControls.edit.call(this, COVIDUrl+'SpUser/getrow', { uuid }, { ids:({ comp_id })=>({ comp_id }), click:()=>{
				this.signatureParam.uuid = uuid
				$fn.disabledRows(formData, 'emplName', true)
				$fn.getRow(formData,'password').required = false
				$fn.getRow(formData,'confirm_password').required = false
			} }),
			
			(+status === -1) && this.onEnabled( 1, uuid ),		// 启用
			(+status === 0) && this.onEnabled( 0, uuid ),		// 禁用
		]
	}
	// 操作按钮
	controls = () => [
		// 添加
		Controls.add.call(this, { 
			click: ()=>{
				const { formData } = this.state
				$fn.disabledRows(formData, 'empl_name', false)
				$fn.getRow(formData,'password').required = true
				$fn.getRow(formData,'confirm_password').required = true
				this.setState({ formData })
			}
		}),			
		this.onEnabled(1),					// 启用
		this.onEnabled(0),					// 禁用
	]
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, api:COVIDUrl+'SpUser/status', param:{status:enabled===1?0:-1}, idStr:'uuids', isKeys: !uuid })

	render(){
		return (
			<>
				<Page title='用户管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ width:800, labelWidth:'s4', api: COVIDUrl+'SpUser/create', editApi:COVIDUrl+'SpUser/update', onRefresh:()=>{
					$fn.dataRemove('employee/select')
				} }) }
			</>
		)
	}
}

export default Index